package com.lyn.watches;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 复古表盘View
 */
public class WatchesView2 extends View {
    private Paint paint;
    private Context context;

    //时
    private volatile int timeAngle;
    //分
    private volatile int minuteAngle;
    //秒
    private volatile int secondAngle;


    private Date date;

    private SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss");

    public WatchesView2(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public WatchesView2(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }



    private void init() {
        date = new Date();

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(2);
        paint.setTextSize(30);
        paint.setStyle(Paint.Style.STROKE);
        paint.setTextAlign(Paint.Align.CENTER);
        Typeface typeface = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
        paint.setTypeface(typeface);

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        invalidate();
        date.setTime(System.currentTimeMillis());

        String format = this.format.format(date);
        String[] split = format.split(":");

        secondAngle = Integer.parseInt(split[2]);
        secondAngle = 360 / 60 * secondAngle - 90;
        minuteAngle = Integer.parseInt(split[1]);
        minuteAngle = 360 / 60 * minuteAngle - 90;
        timeAngle = Integer.parseInt(split[0]);
        timeAngle = (360 / 12 * timeAngle - 90) + 360 / 12 / 60;


        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(4);
        int width = canvas.getWidth();
        int height = canvas.getHeight();

        int centerX = width / 2;
        int centerY = height / 2;

        canvas.drawRGB(45, 74, 106);

        //外圆半径
        int r = centerX - 30;
        //刻度条内圆半径
        int r2 = r - 8;

        paint.setColor(Color.WHITE);
        canvas.drawCircle(centerX, centerY, r, paint);

        for (int i = 0; i < 60; i++) {
            int angle = i * 6;
            boolean isCrude = false;
            if (angle == 0
                    || angle == 30
                    || angle == 60
                    || angle == 90
                    || angle == 120
                    || angle == 150
                    || angle == 180
                    || angle == 210
                    || angle == 240
                    || angle == 270
                    || angle == 300
                    || angle == 330) {
                isCrude = true;
            }
            int x = (int) (r * Math.cos(angle * Math.PI / 180) + centerX);
            int y = (int) (r * Math.sin(angle * Math.PI / 180) + centerY);

            int x2 = (int) ((isCrude ? r2 - 5 : r2) * Math.cos(angle * Math.PI / 180) + centerX);
            int y2 = (int) ((isCrude ? r2 - 5 : r2) * Math.sin(angle * Math.PI / 180) + centerY);

            int x3 = (int) ((isCrude ? r2 - 18 : r2) * Math.cos(angle * Math.PI / 180) + centerX);
            int y3 = (int) ((isCrude ? r2 - 18 : r2) * Math.sin(angle * Math.PI / 180) + centerY);

            paint.setStyle(Paint.Style.FILL);
            if (angle == 0) {
                canvas.drawText(3 + "", x3, baseLine(y3), paint);
            }
            if (angle == 30) {
                canvas.drawText(4 + "", x3, baseLine(y3), paint);
            }
            if (angle == 60) {
                canvas.drawText(5 + "", x3, baseLine(y3), paint);
            }
            if (angle == 90) {
                canvas.drawText(6 + "", x3, baseLine(y3), paint);
            }
            if (angle == 120) {
                canvas.drawText(7 + "", x3, baseLine(y3), paint);
            }
            if (angle == 150) {
                canvas.drawText(8 + "", x3, baseLine(y3), paint);
            }
            if (angle == 180) {
                canvas.drawText(9 + "", x3, baseLine(y3), paint);
            }
            if (angle == 210) {
                canvas.drawText(10 + "", x3, baseLine(y3), paint);
            }
            if (angle == 240) {
                canvas.drawText(11 + "", x3, baseLine(y3), paint);
            }
            if (angle == 270) {
                canvas.drawText(12 + "", x3, baseLine(y3), paint);
            }
            if (angle == 300) {
                canvas.drawText(1 + "", x3, baseLine(y3), paint);
            }
            if (angle == 330) {
                canvas.drawText(2 + "", x3, baseLine(y3), paint);
            }

            if (isCrude) {
                paint.setStrokeWidth(4.5f);
            } else {
                paint.setStrokeWidth(2.5f);
            }
            canvas.drawLine(x, y, x2, y2, paint);
        }


        //时针
        int TimeX = (int) ((r2 / 6 * 3) * Math.cos(timeAngle * Math.PI / 180) + centerX);
        int TimeY = (int) ((r2 / 6 * 3) * Math.sin(timeAngle * Math.PI / 180) + centerY);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(context.getResources().getColor(R.color.cyanBlueColor));
        paint.setStrokeWidth(12);
        canvas.drawLine(centerX, centerY, TimeX, TimeY, paint);
        canvas.drawCircle(TimeX, TimeY, 6f, paint);
        int antiTimeX = (int) (20 * Math.cos((timeAngle-180) * Math.PI / 180) + centerX);
        int antiTimeY = (int) (20 * Math.sin((timeAngle-180) * Math.PI / 180) + centerY);
        canvas.drawLine(centerX,centerY,antiTimeX,antiTimeY,paint);
        canvas.drawCircle( antiTimeX,antiTimeY,6f, paint);

        //分针
        int MinuteX = (int) ((r2 / 6 * 4) * Math.cos(minuteAngle * Math.PI / 180) + centerX);
        int MinuteY = (int) ((r2 / 6 * 4) * Math.sin(minuteAngle * Math.PI / 180) + centerY);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(9);
        canvas.drawLine(centerX, centerY, MinuteX, MinuteY, paint);
        canvas.drawCircle(MinuteX, MinuteY, 4.5f, paint);
        int antiMinuteX = (int) (27 * Math.cos((minuteAngle-180) * Math.PI / 180) + centerX);
        int antiMinuteY = (int) (27 * Math.sin((minuteAngle-180) * Math.PI / 180) + centerY);
        canvas.drawLine(centerX,centerY,antiMinuteX,antiMinuteY,paint);
        canvas.drawCircle( antiMinuteX,antiMinuteY,4.5f, paint);

        //秒针
        int secondX = (int) ((r2 - 8) * Math.cos(secondAngle * Math.PI / 180) + centerX);
        int secondY = (int) ((r2 - 8) * Math.sin(secondAngle * Math.PI / 180) + centerY);
        paint.setColor(context.getResources().getColor(R.color.orangeColor));
        paint.setStrokeWidth(5);
        canvas.drawLine(centerX, centerY, secondX, secondY, paint);
        canvas.drawCircle(secondX,secondY,2.5f,paint);
        int antiSecondX = (int) (30 * Math.cos((secondAngle-180) * Math.PI / 180) + centerX);
        int antiSecondY = (int) (30 * Math.sin((secondAngle-180) * Math.PI / 180) + centerY);
        canvas.drawLine(centerX,centerY,antiSecondX,antiSecondY,paint);
        canvas.drawCircle( antiSecondX,antiSecondY,2.5f, paint);

        //指针中心白小圆
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(context.getResources().getColor(R.color.orangeColor));
        canvas.drawCircle(centerX, centerY, 13, paint);


    }

    private float baseLine(int y) {
        //计算baseline
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        float distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        float baseline = y + distance;
        return baseline;
    }
}
